package com.example.food_contract_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.food_contract_system.domain.Food;
import com.example.food_contract_system.service.FoodService;
import com.example.food_contract_system.mapper.FoodMapper;
import com.example.food_contract_system.util.R;
import com.example.food_contract_system.vo.FoodByImgListVo;
import com.example.food_contract_system.vo.GetFoodAllVo;
import com.example.food_contract_system.vo.SelectByKeyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Lhy
* @description 针对表【food】的数据库操作Service实现
* @createDate 2023-07-06 09:22:12
*/
@Service
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService{

    @Autowired
    private FoodMapper foodMapper;

    @Override
    public R getFoodAll() {
        List<Food> foods = foodMapper.selectList(null);
        if (foods.size() > 0){
            List<GetFoodAllVo> foodAllVos = foods.stream().map(food -> {
                GetFoodAllVo getFoodAllVo = new GetFoodAllVo();

                getFoodAllVo.setFId(food.getFId());
                getFoodAllVo.setFName(food.getFName());
                getFoodAllVo.setFdate(food.getFNumber());
                getFoodAllVo.setFRecommendStatus(food.getFRecommendStatus());

                DecimalFormat df = new DecimalFormat("#.00");
                String formattedNumber = df.format(food.getFPrice());
                getFoodAllVo.setFPrice(formattedNumber);

                getFoodAllVo.setFType(food.getFType());

                getFoodAllVo.setFNum(food.getFNum());
                getFoodAllVo.setDate(food.getFDate());
                getFoodAllVo.setFImg(food.getFImg().split(",")[0]);

                return getFoodAllVo;
            }).collect(Collectors.toList());

            if (foodAllVos.size() > 0){
                return R.ok().put("data",foodAllVos);
            }
        }
        return R.error();
    }

    @Override
    public R selectByKey(SelectByKeyVo selectByKeyVo) {
        List<Food> foods = new ArrayList<>();
        if (selectByKeyVo.getType().equals("")){
            foods = foodMapper.selectByName(selectByKeyVo.getName());
        }else if (selectByKeyVo.getName().equals("")){
            foods = foodMapper.selectByType(selectByKeyVo.getType());
        }else{
            foods = foodMapper.selectByKey(selectByKeyVo.getName(),selectByKeyVo.getType());
        }

        if (foods.size() > 0){
            List<GetFoodAllVo> foodAllVos = foods.stream().map(food -> {
                GetFoodAllVo getFoodAllVo = new GetFoodAllVo();

                getFoodAllVo.setFId(food.getFId());
                getFoodAllVo.setFName(food.getFName());
                getFoodAllVo.setFdate(food.getFNumber());
                getFoodAllVo.setFPrice(String.valueOf(food.getFPrice()));
                getFoodAllVo.setFType(food.getFType());
                getFoodAllVo.setFNum(food.getFNum());
                getFoodAllVo.setFImg(food.getFImg().split(",")[0]);

                return getFoodAllVo;
            }).collect(Collectors.toList());

            if (foodAllVos.size() > 0){
                return R.ok().put("data",foodAllVos);
            }
        }
        return R.error();
    }

    @Override
    public R getFoodListByImgList(Integer id) {
        Food food = foodMapper.selectOne(new QueryWrapper<Food>()
                .eq("f_id",id));
        if (food != null){
            FoodByImgListVo getFoodAllVo = new FoodByImgListVo();

            getFoodAllVo.setFId(food.getFId());
            getFoodAllVo.setFName(food.getFName());

            DecimalFormat df = new DecimalFormat("#.00");
            String formattedNumber = df.format(food.getFPrice());
            getFoodAllVo.setFPrice(formattedNumber);

            getFoodAllVo.setFType(food.getFType());
            getFoodAllVo.setFNum(food.getFNum());
            getFoodAllVo.setFDate(food.getFDate());
            getFoodAllVo.setFDesc(food.getFDesc());

            String[] strings = food.getFImg().split(",");

            List<String> imgList = new ArrayList<>();
            for (int i = 0; i < strings.length; i++) {
                imgList.add(strings[i]);
            }
            getFoodAllVo.setFImg(imgList);

            return R.ok().put("data",getFoodAllVo);
        }
        return R.error();
    }

    @Override
    public R deleteProduct(Integer id) {
        int i = foodMapper.deleteByIdByMyself(id);
        if (i > 0){
            return R.ok().put("msg","删除成功!");
        }
        return R.error();
    }
}




